﻿/*
Copyright 2010 Shane Ng

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Web;
using java.io;
using java.util;

namespace Jeans {
    public class HttpServletRequestAdapter : javax.servlet.http.HttpServletRequest {
        private HttpContext context;
        private HttpRequest Request{
            get {
                return context.Request;
            }
        }
        public HttpServletRequestAdapter(HttpContext context) {
            this.context = context;
        }

        public string getAuthType() {
            throw new NotImplementedException();
        }

        public string getContextPath() {
            return context.Request.ApplicationPath;
        }

        public javax.servlet.http.Cookie[] getCookies() {
            return this.Request.Cookies
                               .Cast<HttpCookie>()
                               .Select(cookie => new javax.servlet.http.Cookie(cookie.Name, cookie.Value))
                               .ToArray();
        }

        public long getDateHeader(string str) {
            return (long)DateTime.Parse(this.Request.Headers[str]).Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }

        public string getHeader(string str) {
            return context.Request.Headers[str];
        }

        public java.util.Enumeration getHeaderNames() {
            return new JavaEnumerator<string>(this.Request.Headers.AllKeys);
        }

        public java.util.Enumeration getHeaders(string str) {
            return new JavaEnumerator<string>(this.Request.Headers.GetValues(str));
        }

        public int getIntHeader(string str) {
            return Convert.ToInt32(this.getHeader(str));
        }

        public string getMethod() {
            return this.Request.HttpMethod;
        }

        public string getPathInfo() {
            throw new NotImplementedException();
        }

        public string getPathTranslated() {
            throw new NotImplementedException();
        }

        public string getQueryString() {
            return this.Request.Url.Query;
        }

        public string getRemoteUser() {
            throw new NotImplementedException();
        }

        public string getRequestURI() {
            return this.Request.Url.AbsolutePath;
        }

        public java.lang.StringBuffer getRequestURL() {
            return new java.lang.StringBuffer(this.Request.Url.GetLeftPart(UriPartial.Path));
        }

        public string getRequestedSessionId() {
            return context.Session.SessionID;
        }

        public string getServletPath() {
            throw new NotImplementedException();
        }

        public javax.servlet.http.HttpSession getSession(bool b) {
            return new HttpSessionAdapter(context.Session);
        }

        public javax.servlet.http.HttpSession getSession() {
            return new HttpSessionAdapter(context.Session);
        }

        public java.security.Principal getUserPrincipal() {
            throw new NotImplementedException();
        }

        public bool isRequestedSessionIdFromCookie() {
            return true;
        }

        public bool isRequestedSessionIdFromURL() {
            return false;
        }

        public bool isRequestedSessionIdFromUrl() {
            return false;
        }

        public bool isRequestedSessionIdValid() {
            throw new NotImplementedException();
        }

        public bool isUserInRole(string str) {
            throw new NotImplementedException();
        }

        public object getAttribute(string str) {
            return context.Items[str];
        }

        public java.util.Enumeration getAttributeNames() {
            return new JavaEnumerator<string>(context.Items.Keys.Cast<string>());
        }

        public string getCharacterEncoding() {
            return Request.ContentEncoding.EncodingName;
        }

        public int getContentLength() {
            return Request.ContentLength;
        }

        public string getContentType() {
            return Request.ContentType;
        }

        public javax.servlet.ServletInputStream getInputStream() {
            return new ServletInputStreamAdapter(new JavaInputStream(context.Request.InputStream));
        }

        public string getLocalAddr() {
            return Dns.GetHostEntry(context.Server.MachineName)
                      .AddressList
                      .First(x => x.AddressFamily == AddressFamily.InterNetwork)
                      .ToString();
        }

        public string getLocalName() {
            return context.Server.MachineName;
        }

        public int getLocalPort() {
            return context.Request.Url.Port;
        }

        public java.util.Locale getLocale() {
            throw new NotImplementedException();
        }

        public java.util.Enumeration getLocales() {
            throw new NotImplementedException();
        }

        public string getParameter(string str) {
            return Request.Params[str];
        }

        public java.util.Map getParameterMap() {
            Map result = new HashMap();
            foreach (var key in Request.Params.AllKeys) {
                var values = Request.Params.GetValues(key);
                if (values.Length == 1) {
                    result.put(key, values[0]);
                }
                else {
                    result.put(key, values);
                }
            }
            return result;
        }

        public java.util.Enumeration getParameterNames() {
            return new JavaEnumerator<string>(Request.Params.AllKeys);
        }

        public string[] getParameterValues(string str) {
            return Request.Params.GetValues(str);
        }

        public string getProtocol() {
            throw new NotImplementedException();
        }

        public java.io.BufferedReader getReader() {
            return new BufferedReader(new InputStreamReader(new JavaInputStream(Request.InputStream)));
        }

        public string getRealPath(string str) {
            return context.Server.MapPath(str);
        }

        public string getRemoteAddr() {
            return Request.UserHostAddress;
        }

        public string getRemoteHost() {
            return Request.UserHostName;
        }

        public int getRemotePort() {
            throw new NotImplementedException();
        }

        public javax.servlet.RequestDispatcher getRequestDispatcher(string str) {
            if (str[0] == '/') {
                return ServletContextAdapter.Instance.getRequestDispatcher(str);
            }
            else {
                return ServletContextAdapter.Instance.getRequestDispatcher(Request.Url.AbsolutePath + str);
            }
        }

        public string getScheme() {
            return Request.Url.Scheme;
        }

        public string getServerName() {
            return Request.Url.Host;
        }

        public int getServerPort() {
            return Request.Url.Port;
        }

        public bool isSecure() {
            return Request.Url.Scheme.EndsWith("s");
        }

        public void removeAttribute(string str) {
            context.Items.Remove(str);
        }

        public void setAttribute(string str, object obj) {
            context.Items[str] = obj;
        }

        public void setCharacterEncoding(string str) {
            Request.ContentEncoding = Encoding.GetEncoding(str);
        }
    }

}